<?php

/**
 * Some usefull form helper
 *
 * @package    symfony
 * @subpackage helper
 * @author     Gordon Franke <gfranke@nevalon.de>
 * @link       http://www.nevalon.de
 */
class gfFormHelper
{
  /**
   * Add * to required field labels
   *
   * @param sfForm $form
   * @param string $symbol
   * @param string $title
   *
   * @return void
   */
  public static function addRequiredToLabel(sfForm $form, $symbol = '*', $title = 'Ce champ est requis.')
  {
    $widgetSchema = $form->getWidgetSchema();
    $validatorSchema = $form->getValidatorSchema();

    foreach($form->getFormFieldSchema()->getWidget()->getFields() as $key => $object)
    {
      $label = $form->getFormFieldSchema()->offsetGet($key)->renderLabelName();
      if(isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('required') == true) {
        $label .= '&nbsp;<font title="' . $widgetSchema->getFormFormatter()->translate($title) . '" color="red">' . $symbol . '</font>';
      }
      $widgetSchema->setLabel($key, $label);
    }
  }
  public static function addValidateOption(sfForm $form)
  {
    $widgetSchema = $form->getWidgetSchema();
    $validatorSchema = $form->getValidatorSchema();

    foreach($form->getFormFieldSchema()->getWidget()->getFields() as $key => $object)
    {
      if(isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('required') == true) {
          $widgetSchema[$key]->setAttribute('class', 'required');
      }
      if(isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('max_length')) {
          $widgetSchema[$key]->setAttribute("maxlength", $validatorSchema[$key]->getOption('max_length'));
      }
      if(isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('minlength')) {
          $widgetSchema[$key]->setAttribute("minlength", $validatorSchema[$key]->getOption('minlength'));
      }
    }
  }
}